begintownscript;

variables;
short creature_count = 0;
short choice = 0;
short pc_counter = 0;
short lava_count = 0;

body;

beginstate 0; //INIT_STATE
	if (get_flag(9,0) != 1) {
		message_dialog("Brother Oakleaf leads you into the cave's entrance, eventually pausing at a sharp bend in the tunnel. He looks like he's planning what to do next.","Eventually, he beckons you over, hopefully to let you know what's next.");
		set_flag(9,0,1);
		}
	if (get_flag( 9,1) != 1) {
		activate_hidden_group(1);
		}
	if(get_flag(9,15) == 1) {
		set_floor(50,22,8);
		set_floor(50,21,8);
		set_floor(50,20,8);
		set_floor(50,19,8);
		set_floor(50,18,8);
		set_floor(50,17,8);
		set_floor(50,16,8);
		set_floor(50,15,8);
		set_floor(50,14,8);
		set_floor(50,13,8);
		set_floor(50,12,8);
		set_floor(50,11,8);
		set_floor(50,10,8);
		set_floor(50,9,8);
		set_floor(50,8,8);
		set_floor(50,7,8);
		set_floor(50,6,8);
		set_floor(50,5,8);
		set_floor(50,4,8);
		set_floor(50,3,8);
		force_instant_terrain_redraw();
		}
break;

beginstate 1; //EXIT_STATE

break;

beginstate 2; //START_STATE
	if(get_flag(9,1) != 1) {
		set_state_continue(10);
		}
break;

beginstate 10; //initial cutscene w/ Oakleaf
	pause(10);
	reset_dialog();
	add_dialog_str(0, "Brother Oakleaf begins to look nervous. _In the years before the Order, this cave was a safe haven for the valley's villagers._", 0);
	add_dialog_str(1, "_Now, we believe that the Order of Flame has taken over this cave, perhaps in an attempt to directly invade our valley. Thus, we must move quickly and quietly._", 0);
	add_dialog_str(2, "_I will scout ahead, to make sure that we can proceed through here... if we can reach the end of the cave, there's a tunnel that should lead to a town in the Order of Flame's valley._",0);
	add_dialog_str(3, "_I just hope we were right when we looked through the cave via the Sanctum's scrying pools. Otherwise, this could get difficult._",0);
	run_dialog(1);
	set_character_pose(16,1);
	text_bubble_on_char(16, "Good luck, fellow druids!");
	force_instant_terrain_redraw();
	pause(10);
	put_effect_on_char(16, 8, 10, 1);
	run_animation_sound(4);
	erase_char(16);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	set_flag(9,1,1);
	reset_dialog();
	add_dialog_str(0, "As Brother Oakleaf waves his arms, he suddenly disappears in a flurry of magic! For a brief moment you think he has left you alone to fight the Order of Flame, but your fear proves unnecessary.", 0);
	add_dialog_str(1, "You hear faint footsteps on the cave's floor, and looking at the dust on the ground, you see footprints walking away from where Oakleaf stood. Brother Oakleaf must've cloaked himself somehow.", 0);
	add_dialog_str(2, "If nothing else, he appears to know what he's doing, so you follow his footsteps into the cave.",0);
	run_dialog(1);
break;

beginstate 11; //lizard batch 1
	if(get_flag(9,2) != 1) {
		reset_dialog();
		message_dialog("As you enter a larger cave, you begin to see what Brother Oakleaf was talking about. A large group of lizards has taken up residence on the west side of this cave, and they look angry.","Their hisses soon grow deafening, and you prepare yourselves for a fight.");
		set_flag(9,2,1);
	}
break;

beginstate 12; // cave blockage
	if(get_flag(9,3) != 1) {
		reset_dialog();
		message_dialog("The passage ahead seems completely blocked. You could try to move it, but it would probably take days... there must be another way through the cave.","Of more interest to you, however, is the hissing sound to the southeast of you. It sounds different from most of the lizards you've killed.");
		set_flag(9,3,1);
	}
break;

beginstate 13; //basilisk warning
	if(get_flag(9,4) != 1) {
		reset_dialog();
		message_dialog("Walking further down this passage, you see what looks like a small bat carved out of stone. You also notice a rock formation which looks uncannily like a fire lizard.","The hissing from down the passage grows louder.");
		set_flag(9,4,1);
	}
break;

beginstate 14; //flame lizards group
	if(get_flag(9,5) != 1) {
		reset_dialog();
		message_dialog("Turning up this passage, you hear more slithering and hissing. However, you see Brother Oakleaf's footprints going through here, so it can't be that bad.","You begin to wonder if the lizards can still detect his presence even if they can't see him. Probably not something to dwell on, really.");
		set_flag(9,5,1);
		pause(10);
		reset_dialog();
		message_dialog("To the north you can hear the sound of drakes flapping around a mid-sized cave. It sounds like there's only two, but anything could be down there, really.","Suddenly, a group of lizards dashes around the corner at you! They must've picked up your scent, or worse... maybe they picked up Brother Oakleaf's!");
		activate_hidden_group(10);
	}
break;

beginstate 15; //Lucky Bracelet
	if(get_flag(9,6) != 1) {
		if(get_highest_skill(18) < 5) {
			message_dialog("Walking down this passage, you come to a dead end filled with rubble and loose rocks. It looks like there's nothing of any interest down here.","");
			}
		else {
			message_dialog("Walking down this passage, you come to a dead end filled with rubble and loose rocks. It looks like there's nothing of any interest down here.","However, upon closer inspection, you notice a glint of metal under the rubble. It turns out to be a bracelet, and you pocket it.");
			char_give_item(char_with_highest_skill(18), 296);
			set_flag(9,6,1);
			}
	}
break;

beginstate 20; //2nd oakleaf cutscene
if(get_flag(9,10) != 1) {
	pause(10);
	reset_dialog();
	message_dialog("Walking along another tightly winding tunnel, you hear rushed breathing to the north. Fortunately, it doesn't really sound like lizard breath, and you don't hear much hissing.","You aproach cautiously...");
		//march the party up
		pause(5);
		march_party(31,23);
		force_view_center(31,23);
		force_instant_terrain_redraw();
		play_sound(50);
		pause(5);
		march_party(32,22);
		force_view_center(32,22);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(5);
		march_party(33,21);
		force_view_center(33,21);
		force_instant_terrain_redraw();
		play_sound(50);
		pause(5);
		march_party(34,20);
		force_view_center(34,20);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(5);
		march_party(35,19);
		force_view_center(35,19);
		force_instant_terrain_redraw();
		play_sound(50);
		pause(5);
		march_party(36,18);
		force_view_center(36,18);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(5);
		march_party(35,17);
		force_view_center(35,17);
		force_instant_terrain_redraw();
		play_sound(50);

		//Oakleaf reappears
		put_effect_on_space(35, 15, 8, 10, 0);
		run_animation_sound(4);
		activate_hidden_group(3);
		force_instant_terrain_redraw();
		pause(5);

		//conversation
		reset_dialog();
		message_dialog("Brother Oakleaf reappears in front of you in another cloud of magic energy. He's a bit singed and scratched, but he's still standing.","");
		pause(5);
		text_bubble_on_char(20, "I must show you something.");
		force_instant_terrain_redraw();
		pause(15);
		erase_text_bubbles();
		force_instant_terrain_redraw();
		text_bubble_on_char(20, "Follow me.");
		force_instant_terrain_redraw();
		pause(15);
		erase_text_bubbles();
		force_instant_terrain_redraw();

		//Lead them further
		set_character_facing(20,2);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(20,34,15);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(20,33,15);
		march_party(35,16);
		force_view_center(35,16);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(5);
		relocate_character(20,32,15);
		march_party(34,15);
		force_view_center(34,15);
		force_instant_terrain_redraw();
		play_sound(50);
		pause(5);
		set_character_facing(20,1);
		relocate_character(20,31,14);
		march_party(33,15);
		force_view_center(33,15);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(5);
		set_character_facing(20,6);
		march_party(32,15);
		force_view_center(32,15);
		force_instant_terrain_redraw();
		play_sound(50);
		pause(10);

		//more talking
		reset_dialog();
		message_dialog("Brother Oakleaf leans back on the cave wall, clearly exhausted. _You might want to see this. From here we can see our enemy's temple._","_To the southeast of it, there's a small town. That's where we should end up, and then we can raid the temple._");

		//look down & over
		pause(10);
		march_party(32,14);
		force_view_center(32,14);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(10);
		force_view_center(32,13);
		force_instant_terrain_redraw();
		pause(10);

		//Kharprev speaks
		reset_dialog();
		message_dialog("As you look down into the lava-filled valley, you can't help but notice how much smoke fills the sky, and how pitiful the small towns look.","However, your attention is grabbed by the immense temple, and the bellowing voice which begins to speak from its center.");
		pause(10);
		put_boom_on_space(30, 4, 0, 0);
		run_animation_sound(152);
		text_bubble_on_char(27, "RALLY THE HORDE!!");
		force_instant_terrain_redraw();
		pause(25);
		erase_text_bubbles();
		force_instant_terrain_redraw();
		text_bubble_on_char(27, "WE MARCH TONIGHT!!");
		force_instant_terrain_redraw();
		pause(25);
		erase_text_bubbles();
		force_instant_terrain_redraw();
		text_bubble_on_char(27, "KHARPREV HAS SPOKEN!!");
		force_instant_terrain_redraw();
		pause(25);
		erase_text_bubbles();
		force_instant_terrain_redraw();
		put_boom_on_space(30, 4, 0, 0);
		run_animation_sound(152);
		pause(25);

		//Oakleaf's wrapup
		force_view_center(32,14);
		force_instant_terrain_redraw();
		reset_dialog();
		add_dialog_str(0 ,"Oakleaf turns to you with a worried look on his face. Clearly, this isn't good for the Order.",0);
		add_dialog_str(1, "_We must move even swifter than before. The fate of the Order depends on it, for if Kharprev invades our valley while Ephesos is gone, we will fall._", 0);
		add_dialog_str(2, "_Our best hope is to run into the temple once their troops have left, and face Kharprev head-on. It's a long shot, but we may yet manage._", 0);
		add_dialog_str(3, "_I'll scout ahead again. Don't worry about finding me, I'll find you._", 0);
		run_dialog(1);

		//Exit Oakleaf
		pause(10);
		set_character_pose(20,1);
		text_bubble_on_char(20, "Good luck!");
		force_instant_terrain_redraw();
		pause(10);
		put_effect_on_char(20, 8, 10, 1);
		run_animation_sound(4);
		erase_char(20);
		erase_text_bubbles();
		force_instant_terrain_redraw();
		pause(10);
		reset_dialog();
		message_dialog("With that, Oakleaf disappears again, and you hear his footsteps move off to the south.", "Not good. It appears that your mission just became even more urgent, and the timing looks like it'll be even more difficult than before.");
		block_entry(1);
		set_flag(9,10,1);
	}
break;

beginstate 21; // more hissing
	if(get_flag(9,11) != 1) {
		reset_dialog();
		message_dialog("As the cave grows darker and more damp, you hear even more hissing to the south. To add to that, it's getting much warmer than it was at the entrance.","In fact, you smell smoke.");
		set_flag(9,11,1);
	}
break;

beginstate 22; //hydra pyromancer intro
	if(get_flag(9,12) != 1) {
		reset_dialog();
		message_dialog("You move further into the cave, and the hissing suddenly stops. However, the smoke smell has grown more intense, and you see streaks of flame occasionally shooting across the cave ahead.","Deciding to play it safe, you crouch behind some stalagmites and look ahead.");
		
		//Oakleaf reappears
		put_effect_on_space(33, 44, 8, 10, 0);
		run_animation_sound(4);
		activate_hidden_group(4);
		force_instant_terrain_redraw();
		pause(10);
		reset_dialog();
		message_dialog("It's Brother Oakleaf again, and he looks more burnt than before. He frantically points deeper into the cave, yelling for you to run.","Thinking he means to continue on without him, you run into the middle of the cave while he hides behind a stalagmite.");
		activate_hidden_group(2);
		force_instant_terrain_redraw();

		//run into the middle of the cave...
		pause(5);
		march_party(36,38);
		force_view_center(36,38);
		force_instant_terrain_redraw();
		play_sound(50);
		pause(5);
		march_party(35,39);
		force_view_center(35,39);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(5);
		march_party(35,40);
		force_view_center(35,40);
		force_instant_terrain_redraw();
		play_sound(50);
		pause(5);
		march_party(35,41);
		force_view_center(35,41);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(5);
		march_party(35,42);
		force_view_center(35,42);
		force_instant_terrain_redraw();
		play_sound(50);
		pause(5);
		march_party(35,43);
		force_view_center(35,43);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(5);
		reset_dialog();
		message_dialog("As you reach Oakleaf, you notice what he's been hiding from. A lighter-than-usual hydra stands in the middle of the cave, and it appears to have an aura of fire around it.","Stranger still, it looks like it's about to cast a spell. It bellows, and a jet of flame strikes Oakleaf in the chest.");
		pause(10);
		put_straight_zap(40, 44, 33, 44, 0);
		put_boom_on_char(30, 1, 0);
		run_animation_sound(51);
		pause(5);
		text_bubble_on_char(30, "AGH!!");
		force_instant_terrain_redraw();
		pause(15);
		erase_text_bubbles();
		force_instant_terrain_redraw();
		pause(15);

		//Oakleaf flees again
		reset_dialog();
		message_dialog("Oakleaf yells in pain, and turns himself invisible again. You hear his rushed footsteps leave the cave chamber.","Meanwhile, the hydra turns its attention to you.");
		pause(5);
		set_character_pose(30,1);
		force_instant_terrain_redraw();
		pause(5);
		put_effect_on_char(30, 8, 10, 1);
		run_animation_sound(4);
		erase_char(30);
		force_instant_terrain_redraw();
		set_char_script_mode(15, 2);
		pause(20);

		//hydra intro
		reset_dialog();
		add_dialog_str(0, "The hydra stares at you with each of its heads, hate filling its glare. Stranger yet, it starts to speak.",0);
		add_dialog_str(1, "_Sssssssss,_ the hydra says. _I am Sschhazsss. You will perissssssssh here now._",0);
		add_dialog_str(2, "The hydra's skin starts to glow more. This could get unpleasant. Then you notice a wand clutched in its mouth, which also starts to glow...", 0);
		run_dialog(1);
		block_entry(1);

		set_flag(9,12,1);
	}
break;

beginstate 23; 
	if(get_flag(9,13) != 1) {
		reset_dialog();
		message_dialog("As you leave the dead hydra mage behind, you hear light footsteps behind you. This time, nobody appears, but the footsteps stay with you.","You guess that it's Brother Oakleaf, as you hear him grumbling about his wounds. He chooses to stay invisible.");
		set_flag(9,13,1);
	}
break;

beginstate 25; //Final exit
	if(get_flag(9,15) != 1) {
		reset_dialog();
		message_dialog("The tunnel you're walking down opens up into a lava-filled chamber, with a set of stairs in the middle of it. The heat is almost unbearable, but you still walk up to the edge of the lava.","");

		//march into cave
		pause(5);
		march_party(48,26);
		force_view_center(48,26);
		force_instant_terrain_redraw();
		play_sound(50);
		pause(5);
		march_party(48,25);
		force_view_center(48,25);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(5);
		march_party(47,24);
		force_view_center(47,24);
		force_instant_terrain_redraw();
		play_sound(50);
		pause(5);
		march_party(46,23);
		force_view_center(46,23);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(5);
		march_party(46,22);
		force_view_center(46,22);
		force_instant_terrain_redraw();
		play_sound(50);
		pause(5);
		march_party(46,21);
		force_view_center(46,21);
		force_instant_terrain_redraw();
		play_sound(49);
		pause(5);

		reset_dialog();
		message_dialog("As you ponder how to cross the lava, Brother Oakleaf's footsteps come up behind you again.","He jumps back into visibility, and starts muttering another spell.");

		//Oakleaf reappears w/ a spell
		put_effect_on_space(50, 23, 8, 10, 0);
		run_animation_sound(4);
		activate_hidden_group(5);
		force_instant_terrain_redraw();
		pause(15);
		set_character_pose(31,1);
		pause(5);
		put_effect_on_char(31, 6, 10, 1);
		run_animation_sound(52);
		pause(5);
		put_boom_on_space(50, 22, 5, 0);
		put_boom_on_space(50, 21, 5, 0);
		put_boom_on_space(50, 20, 5, 0);
		put_boom_on_space(50, 19, 5, 0);
		put_boom_on_space(50, 18, 5, 0);
		put_boom_on_space(50, 17, 5, 0);
		put_boom_on_space(50, 16, 5, 0);
		put_boom_on_space(50, 15, 5, 0);
		put_boom_on_space(50, 14, 5, 0);
		put_boom_on_space(50, 13, 5, 0);
		put_boom_on_space(50, 12, 5, 0);
		put_boom_on_space(50, 11, 5, 0);
		put_boom_on_space(50, 10, 5, 0);
		put_boom_on_space(50, 9, 5, 0);
		put_boom_on_space(50, 8, 5, 0);
		put_boom_on_space(50, 7, 5, 0);
		put_boom_on_space(50, 6, 5, 0);
		put_boom_on_space(50, 5, 5, 0);
		put_boom_on_space(50, 4, 5, 0);
		put_boom_on_space(50, 3, 5, 0);
		run_animation_sound(52);
		set_floor(50,22,8);
		set_floor(50,21,8);
		set_floor(50,20,8);
		set_floor(50,19,8);
		set_floor(50,18,8);
		set_floor(50,17,8);
		set_floor(50,16,8);
		set_floor(50,15,8);
		set_floor(50,14,8);
		set_floor(50,13,8);
		set_floor(50,12,8);
		set_floor(50,11,8);
		set_floor(50,10,8);
		set_floor(50,9,8);
		set_floor(50,8,8);
		set_floor(50,7,8);
		set_floor(50,6,8);
		set_floor(50,5,8);
		set_floor(50,4,8);
		set_floor(50,3,8);
		force_instant_terrain_redraw();
		pause(5);
		set_character_pose(31, 0);
		force_instant_terrain_redraw();
		pause(20);
		
		//message
		reset_dialog();
		message_dialog("As the lava cools to stone, Oakleaf turns to you and smiles. _Now that we can cross the lava, shall we continue?_", "The partially-toasted druid walks to the stairs, and motions for you to follow.");
		
		//exit Oakleaf
		pause(10);
		relocate_character(31,50,22);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,21);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,20);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,19);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,18);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,17);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,16);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,15);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,14);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,13);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,12);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,11);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,10);
		force_instant_terrain_redraw();
		pause(5);
		relocate_character(31,50,9);
		force_instant_terrain_redraw();
		pause(5);
		erase_char(31);
		force_instant_terrain_redraw();

		block_entry(1);
		set_flag(9,15,1);
		}
break;

beginstate 30; //tunnel out to flamevalley
	reset_dialog();
	add_dialog_str(0, "You descend the numerous flights of stairs, and meet Brother Oakleaf at the bottom. He leads you down the tunnel, and you come up to a giant metal door.",0);
	add_dialog_str(1, "_On the other side of this door is a town controlled by the Order of Flame. I can't vouch for who or what is in it, nor can I really say if we'll get by them._",0);
	add_dialog_str(2, "_Still, we need to press on, because we need to find Ephesos. So, on the count of three, we make a break for it._", 0);
	add_dialog_str(3, "Oakleaf slowly counts to three, and all of you run through the door as quickly as possible...", 0);
	run_dialog(1);
	play_sound(160);
	change_outdoor_location(1,1,37,28);
break;

beginstate 40; //blockexit
	block_entry(1);
	message_dialog("You can't turn back now... you've got to press on with Brother Oakleaf, and try to rescue Ephesos!","");
break;